<html>
<head><meta charset="utf-8"><title>Implementing Trait for all enums · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Implementing.20Trait.20for.20all.20enums.html">Implementing Trait for all enums</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243308145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Implementing%20Trait%20for%20all%20enums/near/243308145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Wagner-Hall <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Implementing.20Trait.20for.20all.20enums.html#243308145">(Jun 20 2021 at 15:39)</a>:</h4>
<p>Thanks so much for the feedback on <a href="https://github.com/rust-lang/rust/pull/81642">https://github.com/rust-lang/rust/pull/81642</a> (thanks for taking a look!) - I'm trying to work out how to go about implementing a trait for all enums (which meet certain trivially detectable properties) automatically... Can anyone give me any pointers?</p>
<p>I don't have much context for how these parts of <code>rustc</code> work, but from grepping around <code>rustc_interface</code>, it looks like the simplest way to go may be to teach the macro expander (in <code>rustc_expand</code>) that it should run a derive macro which only exists for the purposes of rustc and isn't exposed anywhere outside the compiler, even though no derive macro is written on the type?</p>
<p>I also thought about having <code>rustc_ast_lowering</code> just pop out a second <code>Item</code> as a side-effect of lowering <code>enum</code>s, but it looks like that code strongly assumes that one <code>Item</code> is lowered to exactly one <code>Item</code> (which is very reasonable!)</p>
<p>Is there an obviously better approach? Or any other prior art anyone can think of? I can only think of traits which are blanket impl'd (e.g. <code>Any</code>) but <code>enum</code>s AFAIK don't have properties which would allow a blanket impl to target them, or traits which are impl'd on a finite known set of types (e.g. <code>ops::Add</code> for <code>u*</code> types), and not traits which are auto-impl'd for a subset of user-defined types...</p>



<a name="243309183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Implementing%20Trait%20for%20all%20enums/near/243309183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Implementing.20Trait.20for.20all.20enums.html#243309183">(Jun 20 2021 at 16:03)</a>:</h4>
<p>you may want to ask in <a class="stream" data-stream-id="182449" href="/#narrow/stream/182449-t-compiler.2Fhelp">#t-compiler/help</a>  as well</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>